package com.polaris.live.ui.live.proxy

import android.content.Context
import androidx.fragment.app.Fragment
import com.lxj.xpopup.core.BasePopupView
import com.polaris.live.constant.LiveConst.LivePage
import com.polaris.live.dialog.GlobalDialogManager
import com.polaris.live.interface_bag.XpopupCallBackImpl
import com.polaris.live.popup.BeautyPopup
import com.polaris.live.popup.FeedBackPopup
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.popup.VideoMorePopup
import com.polaris.live.ui.live.widget.listener.OnBackPressedListener
import com.polaris.live.ui.live.widget.listener.OnChangeCameraListener
import com.polaris.live.utils.XPopupUtils
import com.polaris.live.utils.autoFragmentHide
import com.polaris.live.utils.zego.beauty.FaceManager
import com.polaris.live.viewmodel.live.LiveViewModel

/**
 * 更多的逻辑
 *
 * @author Created by lucas on 2023/11/10 17:34
 */
class LiveMoreProxy(
    private val context: Context,
    private val fragment: Fragment,
    private val viewModel: LiveViewModel,
    @LivePage
    private val pageType: Int,
    private val onChangeCameraListener: OnChangeCameraListener?,
    private val onBackPressedListener: OnBackPressedListener?,
) {

    private var isBlock = false

    /**
     * 显示视频的更多弹窗
     */
    fun showVideoMore(
        userId: Long? = null,
        userName: String? = null,
        objectId: Long,
        onKeyboardStateListener: ((state: Boolean) -> Unit)? = null,
        mIsOpenGift: Boolean = true,
        mIsOpenSound: Boolean = true,
        onOpenGiftAnimation: ((state: Boolean) -> Unit)? = null,
        onOpenGiftSound: ((state: Boolean) -> Unit)? = null,
        onInvitationFriend: (() -> Unit)? = null, //邀请好友
    ) {
        val popup = VideoMorePopup(
            context,
            pageType,
            isBlock,
            mIsOpenGift,
            mIsOpenSound,
            viewModel,
            onOpenGiftAnimation,
            onOpenGiftSound
        ) {
            when (it) {
                VideoMorePopup.beauty -> {
                    dismiss()

                    XPopupUtils.showViewPopup(
                        BeautyPopup(context) { item, type ->
                            FaceManager.setBeautyEvent(item, type)
                        }.apply {
                            autoFragmentHide(fragment)
                        },
                        lifecycle = fragment.lifecycle
                    )
                }

                VideoMorePopup.turn -> {
                    onChangeCameraListener?.onChangeCamera()
                }

                VideoMorePopup.report -> {
                    if (userId != null && userName != null) {
                        dismiss()
                        GlobalPopupManager.showReport(
                            context,
                            userName,
                            viewModel,
                            userId,
                            objectId,
                            fragment
                        )
                    }
                }

                VideoMorePopup.blackList -> {
                    if (userId != null && userName != null) {
                        dismiss()

                        GlobalDialogManager.showBlackIt(
                            context,
                            viewModel,
                            isBlock,
                            userName,
                            userId,
                            fragment
                        ) {
                            isBlock = it
                        }
                    }
                }

                VideoMorePopup.feedback -> {
                    dismiss()
                    XPopupUtils.showPopup(
                        FeedBackPopup(context, viewModel),
                        {
                            setPopupCallback(object : XpopupCallBackImpl {
                                override fun onCreated(popupView: BasePopupView) {
                                    onKeyboardStateListener?.invoke(false)
                                }

                                override fun onDismiss(popupView: BasePopupView) {
                                    onKeyboardStateListener?.invoke(true)
                                }
                            })
                        },
                        lifecycle = fragment.lifecycle
                    )
                }

                VideoMorePopup.closeVideo -> {
                    dismiss()

                    onBackPressedListener?.onBackPressed()
                }

                VideoMorePopup.invitation_friend -> {
                    dismiss()
                    onInvitationFriend?.invoke()
                }
            }
        }

        XPopupUtils.showViewPopup(popup, lifecycle = fragment.lifecycle)
        popup.autoFragmentHide(fragment)
    }
}